/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author MoroComputer
 */
public class Defesa extends DML {

    private Aluno aluno;
    private String horario;
    private String data;
    private String local;

    public Defesa(Aluno aluno, String horario, String data, String local) {
        this.aluno = aluno;
        this.horario = horario;
        this.data = data;
        this.local = local;
        super.table = "defesa";
    }

    public boolean registrarDefesa() {
        if (this.aluno != null) {
            int idAluno = this.aluno.recuperaIdAlunoPorNome(this.aluno.getNome());
            if (idAluno != 0) {
                try {
                    if (verificarExistenciaDeDefesa(idAluno) == false) {
                        String sql = "insert into " + super.table + "(data,hora,local,aluno_id) values(?,?,?,?);";
                        super.conecta = ConectaBD.getInstance();
                        super.preparedStatement = super.conecta.getConnection().prepareStatement(sql);
                        super.preparedStatement.setString(1, this.data);
                        super.preparedStatement.setString(2, this.horario);
                        super.preparedStatement.setString(3, this.local);
                        super.preparedStatement.setInt(4, idAluno);
                        super.preparedStatement.execute();
                        super.preparedStatement.close();
                        return true;
                    } else {
                        return alterarDadosDeDefesa();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } finally {
                    super.conecta.encerrarConexao();
                }
            } else {
                return false;
            }
        }
        return false;
    }

    public boolean alterarDadosDeDefesa() {
        if (this.aluno != null) {
            int idAluno = this.aluno.recuperaIdAlunoPorNome(this.aluno.getNome());
            if (idAluno != 0) {
                try {
                    String sql = "update " + super.table + " set data=?,hora=?,local=? where aluno_id=" + idAluno + ";";
                    super.conecta = ConectaBD.getInstance();
                    super.preparedStatement = super.conecta.getConnection().prepareStatement(sql);
                    super.preparedStatement.setString(1, this.data);
                    super.preparedStatement.setString(2, this.horario);
                    super.preparedStatement.setString(3, this.local);
                    super.preparedStatement.execute();
                    super.preparedStatement.close();
                    return true;
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } finally {
                    super.conecta.encerrarConexao();
                }
            } else {
                return false;
            }
        }
        return false;
    }

    public boolean verificarExistenciaDeDefesa(int idAluno) {
        if (idAluno != 0) {
            String sql = "select idDefesa from " + super.table + " where aluno_id=" + idAluno + ";";
            try {
                super.conecta = ConectaBD.getInstance();
                super.statement = super.conecta.getConnection().createStatement();
                super.resultSet = super.statement.executeQuery(sql);

                int idDefesa = 0;

                while (super.resultSet.next()) {
                    idDefesa = super.resultSet.getInt(1);
                }
                if (idDefesa != 0) {
                    return true;
                } else {
                    return false;
                }

            } catch (SQLException erroSQL) {

            }
        } else {
            return false;
        }
        return false;
    }

    public ArrayList<String> recuperarDadosDeDefesa() {
        if (this.aluno != null) {
            int idAluno = this.aluno.recuperaIdAlunoPorNome(this.aluno.getNome());
            if (idAluno != 0) {
                String sql = "select data, hora, local from " + super.table + " where aluno_id=" + idAluno + ";";
                try {
                    super.conecta = ConectaBD.getInstance();
                    super.statement = super.conecta.getConnection().createStatement();
                    super.resultSet = super.statement.executeQuery(sql);

                    ArrayList<String> dadosResult = new ArrayList<String>();

                    while (super.resultSet.next()) {
                        String data = super.resultSet.getString(1);
                        String hora = super.resultSet.getString(2);
                        String local = super.resultSet.getString(3);
                        dadosResult.add(data);
                        dadosResult.add(hora);
                        dadosResult.add(local);
                        return dadosResult;
                    }
                } catch (SQLException erroSQL) {

                }
            }
        }
        return null;
    }

    public boolean setarBancaEmDefesa(int idBanca, int idAluno) {
        if (idBanca != 0 && idAluno != 0) {
            try {
                String sql = "update " + super.table + " set banca_idbanca=? where aluno_id=" + idAluno + ";";
                super.conecta = ConectaBD.getInstance();
                super.preparedStatement = super.conecta.getConnection().prepareStatement(sql);
                super.preparedStatement.setInt(1, idBanca);
                super.preparedStatement.execute();
                super.preparedStatement.close();
                return true;
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                super.conecta.encerrarConexao();
            }
        }
        return false;
    }

    public int recuperaIdDefesa(int idAluno) {
        if (idAluno != 0) {
            String sql = "select idDefesa from " + super.table + " where aluno_id=" + idAluno + ";";
            try {
                super.conecta = ConectaBD.getInstance();
                super.statement = super.conecta.getConnection().createStatement();
                super.resultSet = super.statement.executeQuery(sql);

                int dadoResult = 0;

                while (super.resultSet.next()) {
                    dadoResult = super.resultSet.getInt(1);
                    return dadoResult;
                }
            } catch (SQLException erroSQL) {

            }
        }
        return 0;
    }
    public ArrayList<String> recuperarDadosDeDefesa(int idAluno) {
        if (idAluno != 0) {
                String sql = "select data, hora, local from " + super.table + " where aluno_id=" + idAluno + ";";
                try {
                    super.conecta = ConectaBD.getInstance();
                    super.statement = super.conecta.getConnection().createStatement();
                    super.resultSet = super.statement.executeQuery(sql);

                    ArrayList<String> dadosResult = new ArrayList<String>();

                    while (super.resultSet.next()) {
                        String data = super.resultSet.getString(1);
                        String hora = super.resultSet.getString(2);
                        String local = super.resultSet.getString(3);
                        dadosResult.add(data);
                        dadosResult.add(hora);
                        dadosResult.add(local);
                        return dadosResult;
                    }
                } catch (SQLException erroSQL) {

                
            }
        }
        return null;
    }
     public int recuperaIdBanca(int idDefesa) {
        if (idDefesa != 0) {
            String sql = "select banca_idbanca from " + super.table + " where idDefesa=" + idDefesa + ";";
            try {
                super.conecta = ConectaBD.getInstance();
                super.statement = super.conecta.getConnection().createStatement();
                super.resultSet = super.statement.executeQuery(sql);

                int dadoResult = 0;

                while (super.resultSet.next()) {
                    dadoResult = super.resultSet.getInt(1);
                    return dadoResult;
                }
            } catch (SQLException erroSQL) {

            }
        }
        return 0;
    }

     public int recuperaIdAluno(int idBanca) {
        if (idBanca != 0) {
            String sql = "select aluno_id from " + super.table + " where banca_idbanca=" + idBanca + ";";
            try {
                super.conecta = ConectaBD.getInstance();
                super.statement = super.conecta.getConnection().createStatement();
                super.resultSet = super.statement.executeQuery(sql);

                int dadoResult = 0;

                while (super.resultSet.next()) {
                    dadoResult = super.resultSet.getInt(1);
                    return dadoResult;
                }
            } catch (SQLException erroSQL) {

            }
        }
        return 0;
    }
    /**
     * @return the aluno
     */
    public Aluno getAluno() {
        return aluno;
    }

    /**
     * @return the horario
     */
    public String getHorario() {
        return horario;
    }

    /**
     * @return the data
     */
    public String getData() {
        return data;
    }

    /**
     * @return the local
     */
    public String getLocal() {
        return local;
    }

}
